<template>
	<view class="user-list">
		<!-- 模式选择 -->
		<view class="ll">
			<radio-group class="table-mode" @change="radioChange">
				<view>
					<radio :value="1" :checked="1 === modeIndex" /><text>表格 + 表单模式</text>
				</view>
				<view>
					<radio :value="2" :checked="2 === modeIndex" /><text>表格模式</text>
				</view>
			</radio-group>
		</view>
		<view class="jcg_table">
			<view :class="modeIndex == 1 ? '' : 'scroll_right'">
				<scroll-view
					:scroll-x="modeIndex == 1 ? false : true"
				>
					<view class="th">
						<view v-if="modeIndex == 1" class="td" style="width: 58rpx;"></view>
						<template v-for="(th, ind) in thList">
							<view class="td" :key="ind"
								:style="{width: th.width||'220rpx'}" @click="bindSort(th.sortKey)">
								<view v-html="th.text"></view>
							</view>
						</template>
					</view>
					<view v-for="(item, ind) in userList" :key="ind" class="row">
						<view 
							class="tr"
							:class="{disabled:item.disabled}"
						>
							<view class="td" v-if="modeIndex == 1">
								<uni-icons :type="item.expanded ? 'bottom' : 'right'" size="14" color="#666666" @click="expandedChange(item)"></uni-icons>
							</view>
							<template v-for="(th, index) in thList">
								<view class="td" :key="index" :style="{width: th.width||'220rpx'}">
									<text>{{ th.dataKey == 'index'?ind+1 : item[th.dataKey] }}</text>
								</view>
							</template>
						</view> 
						<!-- 表单内容 -->
						<view v-if="modeIndex == 1 && item.expanded" class="table-from">
							<view class="paramBox">
								<!-- <view class="param-title">{{item.name}}</view> -->
								<view v-for="(lb, j) in fromList" :key="j" class="param-item">
									<label>{{ lb.text }}</label>
									<text>{{ item[lb.dataKey]  }}</text>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "jcgTable",
		data() {
			return {
				modeIndex: 1,
				labels: { // 表单label字段含义
					"test1":"列名1",
					"test2":"列名2",
					"test3":"列名3",
					"test4":"列名4",
					"test5":"列名5",
					"test6":"列名6",
				},
				thList: [],
				fromList: [], // 表单内容
				// thList: [ // 默认表单模式三个
				// 	{
				// 		text: '书',	// 字段含义
				// 		dataKey: 'book',    //该列渲染的数据字段 
				// 	},
				userList: [
					{
						"test1":"xxxxx",
						"test2":"xxxxx",
						"test3":"xxxxx",
						"test4":"xxxxx",
						"test5":"xxxxx",
						"test6":"xxxxx",
						"expanded": false  // 打开控制。可以获取后台数据后循环追加这个参数
					},
					{
						"test1":"xxxxx",
						"test2":"xxxxx",
						"test3":"xxxxx",
						"test4":"xxxxx",
						"test5":"xxxxx",
						"test6":"xxxxx",
						"expanded": false  // 打开控制。可以获取后台数据后循环追加这个参数
					},
					{
						"test1":"xxxxx",
						"test2":"xxxxx",
						"test3":"xxxxx",
						"test4":"xxxxx",
						"test5":"xxxxx",
						"test6":"xxxxx",
						"expanded": false  // 打开控制。可以获取后台数据后循环追加这个参数
					},
					{
						"test1":"xxxxx",
						"test2":"xxxxx",
						"test3":"xxxxx",
						"test4":"xxxxx",
						"test5":"xxxxx",
						"test6":"xxxxx",
						"expanded": false  // 打开控制。可以获取后台数据后循环追加这个参数
					},
					{
						"test1":"xxxxx",
						"test2":"xxxxx",
						"test3":"xxxxx",
						"test4":"xxxxx",
						"test5":"xxxxx",
						"test6":"xxxxx",
						"expanded": false  // 打开控制。可以获取后台数据后循环追加这个参数
					},
					{
						"test1":"xxxxx",
						"test2":"xxxxx",
						"test3":"xxxxx",
						"test4":"xxxxx",
						"test5":"xxxxx",
						"test6":"xxxxx",
						"expanded": false  // 打开控制。可以获取后台数据后循环追加这个参数
					}
				]
			};
		},
		mounted() {
			this.modeChange(1)
		},
		methods: {
			// 属性选择事件
			radioChange(e) {
				this.modeIndex = parseInt(e.detail.value)
				this.modeChange(this.modeIndex)
			},
			expandedChange(item) {
				item.expanded = !item.expanded
			},
			modeChange(type) { //  1.表单 2。表格
				this.thList = []
				this.fromList = []
				if (type == 1) {
					// 默认三列
					const keys = Object.keys(this.labels)
					const arr = keys.splice(0, 3)
					arr.forEach((x, i) => {
						this.thList.push({
							text: 'test' + (i + 1),
							dataKey: 'test' + (i + 1),
						})
					})
					keys.forEach((x, i) => {
						this.fromList.push({
							text: 'test' + (i + 4),
							dataKey: 'test' + (i + 4)
						})
					})
				} else if (type == 2) {
					const keys = Object.keys(this.labels)
					keys.forEach((x, i) => {
						this.thList.push({
							text: 'test' + (i + 1),
							dataKey: 'test' + (i + 1),
						})
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user-list {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		background: #fff;
	}
	.table-mode {
		width: 100%;
		display: flex;
		padding: 40rpx 0;
		background: #fff;
		justify-content: space-around;
	}
	.jcg_table {
		background: #fff;
		// margin-top: 20rpx;
		display: flex;
		margin-top: 10px;
    margin-left: 5px;
    border-left: 1px solid #dfe6ec;
		// .scroll_right {
		// 	flex: 1;
		// 	overflow: hidden;
		// }
		.row:nth-child(odd) {
			.tr {
				background-color: #fafafa;
			}
		}
		
		.expanded {
			.tr {
				background-color: #e8f4ff;
			}
		}
		
		.th, .tr {
			display: flex;
			flex-wrap: nowrap;
			// position: relative;

			&:before {
				content: '';
				position: absolute;
				bottom: 0;
				left: 0;
				width: 100%;
				border-bottom: 1px solid #dfe6ec;
			}

			&.disabled {
				opacity: 0.5;
			}

			.td {
				flex-shrink: 0;
				display: flex;
				align-items: center;
				justify-content: center;
				height: 80rpx;
				font-size: 24rpx;
				color: #333;
				padding: 0 14rpx;
				box-sizing: border-box;
				border-bottom: 1px solid #dfe6ec;
				border-right: 1px solid #dfe6ec;
				text-align: center;

				.mark {
					display: flex;
					align-items: center;
					justify-content: center;
					background: #E09F70;
					color: #fff;
					width: 28rpx;
					height: 24rpx;
					border-radius: 5rpx;
					margin-right: 8rpx;
					font-size: 20rpx;
				}

				.txt {
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					/*（多行）*/
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					font-size: 24rpx;
				}
			}
		}

		.th {
			.td {
				color: #8A8F99;
				text-align: center;
				border-right: 1px solid #dfe6ec;
				border-top: 1px solid #dfe6ec;
			}
		}
	}
	.table-from {
		width: 100%;
	}
	.paramBox {
		// margin-top: 30rpx;
	}
	.tabContent {
		background: #fff;
		overflow: hidden;
		min-height: 100vh;
	}
	
	.scroll_right {
		flex: 1;
		overflow: hidden;
	}
	
	.paramBox {
		border-right: 1px solid #dfe6ec;
		border-bottom: 1px solid #dfe6ec;
	}
	
	.param-title {
		background: #f5f5f5;
		height: 80rpx;
		line-height: 80rpx;
		margin: 20rpx 30rpx 0;
		font-size: 28rpx;
		text-indent: 30rpx;
	}
	
	.param-item {
		line-height: 80rpx;
		font-size: 24rpx;
		display: flex;
		padding: 0 10%;
		align-items: center;
		// border-bottom: 1rpx solid #f5f5f5;
		// margin: 0 30rpx;
		// border-left: 1rpx solid #f5f5f5;
		// border-right: 1rpx solid #f5f5f5;
		// padding: 0 30rpx;
	}
	.param-item:first-child{ 
		border-top: 1rpx solid #f5f5f5;
	}
	
	.param-item label {
		display: inline-block;
		width: 50%;
		color: #999;
	}
	.param-item text {
		width: 50%;
	}
	
</style>